package com.xf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xf.common.core.domain.DmpResult;
import com.xf.common.core.entity.QueryRequest;
import com.xf.common.core.entity.dmp.OauthClientDetails;
import com.xf.common.core.exception.FebsException;
import com.xf.service.OauthClientDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @author JoeyLiu
 * @create 2022-04-14 17:06
 */

@Slf4j
@Validated
@RestController
@Api(tags = "客户端管理")
@RequestMapping("client")
public class OauthClientDetailsController {
    @Resource
    private OauthClientDetailsService oauthClientDetailsService;


    @ApiOperation("检查客户端是否存在")
    @GetMapping("check/{clientId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端id", paramType = "path", dataType = "string")
    })
    public DmpResult checkUserName(@NotBlank(message = "{required}") @PathVariable String clientId) {
        OauthClientDetails client = oauthClientDetailsService.findById(clientId);
        if (client == null) {
            return DmpResult.failed();
        }
        return DmpResult.success();
    }

    @ApiOperation("根据id查询")
    @GetMapping("get/{clientId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端id", paramType = "path", dataType = "string")
    })
    public DmpResult getOriginClientSecret(@NotBlank(message = "{required}") @PathVariable String clientId) {
        OauthClientDetails client = this.oauthClientDetailsService.findById(clientId);
        String origin = client != null ? client.getOriginSecret() : StringUtils.EMPTY;
        return DmpResult.data(origin);
    }

    @GetMapping("/getPage")
    @ApiOperation("分页查询")
    public DmpResult<IPage<OauthClientDetails>> oauthCliendetailsList(QueryRequest request, OauthClientDetails oAuthClientDetails) {
        return DmpResult.data(oauthClientDetailsService.findOauthClientDetails(request, oAuthClientDetails));
    }


    @ApiOperation("新增客户端")
    @PostMapping(value = "/add")
    public DmpResult addOauthCliendetails(@Valid @RequestBody OauthClientDetails oAuthClientDetails) throws FebsException {
        try {
            oauthClientDetailsService.createOauthClientDetails(oAuthClientDetails);
            return DmpResult.success("新增客户端成功");
        } catch (Exception e) {
            String message = "新增客户端失败";
            log.error(message, e);
            return DmpResult.failed(message);
        }
    }

    @ApiOperation("删除客户端")
    @PostMapping(value = "/delete")
    public DmpResult deleteOauthCliendetails(@NotBlank(message = "{required}") String clientIds) throws FebsException {
        try {
            oauthClientDetailsService.deleteOauthClientDetails(clientIds);
            return DmpResult.success("删除客户端成功");
        } catch (Exception e) {
            String message = "删除客户端失败";
            log.error(message, e);
            return DmpResult.failed(message);
        }
    }

    @ApiOperation("修改客户端")
    @PostMapping(value = "/update")
    public DmpResult updateOauthCliendetails(@Valid OauthClientDetails oAuthClientDetails) throws FebsException {
        try {
            oauthClientDetailsService.updateOauthClientDetails(oAuthClientDetails);
            return DmpResult.success("修改客户端成功");
        } catch (Exception e) {
            String message = "修改客户端失败";
            log.error(message, e);
            return DmpResult.failed(message);
        }
    }


}
